﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess
{
    public class LocationsDataAccess : DataAccessBase
    {
        public LocationsDataAccess(string dbName, string conn, bool usePredefinedMetadata)
            :base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<Location> SelectLocationsList()
        {
            //Select Tobject List
            List<TObject> tobjList = SelectTObjectsList();

            List<LOCATION> locList = dataContext.LOCATION.ToList();

            //Merge result
            var res = (from tobj in tobjList
                       join loc in locList on tobj.ID equals loc.ID
                       select new Location
                       {
                           DataBaseName = dataBaseName,
                           CLASS_ID = tobj.CLASS_ID,
                           EXT_ID = tobj.EXT_ID,
                           ID = tobj.ID,
                           NAME = tobj.NAME,
                           PARENT_ID = tobj.PARENT_ID,
                           SQUARE = (Single)loc.SQUARE,
                           RESPONSIBLE_PERSON_ID = loc.RESPONSIBLE_PERSON
                       }).ToList();

            return res;
        }

        public Location SelectLocationBy_Id(int Id)
        {
            try
            {
                //Select Tobject part by Id
                TObject basePart = SelectTObject_By_Id(Id);

                List<LOCATION> locPart = dataContext.LOCATION.ToList();

                //Merge result
                var res = (from loc in locPart
                           where loc.ID == basePart.ID
                           select new Location
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = basePart.CLASS_ID,
                               EXT_ID = basePart.EXT_ID,
                               ID = basePart.ID,
                               NAME = basePart.NAME,
                               PARENT_ID = basePart.PARENT_ID,
                               SQUARE = (Single)loc.SQUARE,
                               RESPONSIBLE_PERSON_ID = loc.RESPONSIBLE_PERSON
                           }).FirstOrDefault();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectLocationBy_Id");
            }
        }

        public void InsertLocation(Location loc, ref int generated_Id)
        {
            try
            {
                //Insert the TObject part
                InsertTObject(loc, ref generated_Id);

                int new_Id = generated_Id;
                LOCATION locPart = new LOCATION();
                locPart.TOBJECT = dataContext.TOBJECT.Where(t => t.ID == new_Id).FirstOrDefault();
                locPart.ID = new_Id;
                locPart.SQUARE = loc.SQUARE;
                locPart.RESPONSIBLE_PERSON = loc.RESPONSIBLE_PERSON_ID;

                //Submit changes
                dataContext.AddToLOCATION(locPart);
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while InsertLocation");
            }
        }

        public void UpdateLocation(Location loc)
        {
            try
            {
                //Update the TObject part
                UpdateTObject(loc);

                LOCATION locPart = dataContext.LOCATION.Where(t => t.ID == loc.ID).FirstOrDefault();

                locPart.SQUARE = loc.SQUARE;
                locPart.RESPONSIBLE_PERSON = loc.RESPONSIBLE_PERSON_ID;

                //Submit changes
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while UpdateLocation");
            }
        }

        public void DeleteLocation(Location loc)
        {
            try
            {
                LOCATION locPart = dataContext.LOCATION.Where(t => t.ID == loc.ID).FirstOrDefault();

                dataContext.DeleteObject(locPart);
                dataContext.SaveChanges();


                //Delete the TObject part
                DeleteTObject(loc);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteLocation");
            }
        }
    }
}
